package net.lijingye.education.util;

import net.lijingye.education.entity.Source;
import net.lijingye.education.entity.Topic;
import net.lijingye.education.entity.User;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.vo.PageVO;
import org.springframework.security.access.method.P;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LijingYe on 2016/4/3.
 */
public class CommonUtil {
    public static String getBriefText(String context) {
        if (null == context || context.length() <= 8) {
            return context;
        }
        return context.substring(0, 9) + "...";
    }

    public static String getUserHomeText(User user) {
        if (null == user.getProvince().getName()) {
            return null;
        }
        String home = user.getProvince().getName();
        if (null != user.getCity().getName()) {
            home += " - " + user.getCity().getName();
        }
        return home;
    }

    public static String getUserDisplayName(User user){
        if(null==user.getNickName()){
            return user.getName();
        }
        return user.getNickName();
    }

    public static String getUserGenderText(User user) {
        if (null == user) {
            return null;
        }
        switch (user.getGender()) {
            case 0:
                return "男";
            case 1:
                return "女";
            default:
                break;
        }
        return null;
    }

    public static String getTopicTypeText(Topic topic) {
        if (null == topic) {
            return null;
        }
        switch (topic.getType()) {
            case 1:
                return "重点难点";
            case 2:
                return "扩展阅读";
            default:
                break;
        }
        return null;
    }

    public static Boolean getTopicIsSolvedBoolean(Topic topic) {
        if (null == topic) {
            return null;
        }
        switch (topic.getType()) {
            case 0:
                return false;
            case 1:
                return true;
            default:
                break;
        }
        return null;
    }

    public static String getSourceTypeText(Source source) {
        if (null == source) {
            return null;
        }
        switch (source.getType()) {
            case 1:
                return "教材与参考书目";
            case 2:
                return "经典试题";
            case 3:
                return "面试经";
            default:
                break;
        }
        return null;
    }

    public static SuccessResponse splitPage(List<?> data, String page, Integer pageSize) {
        int currentPage;
        int pageCount = data.size() % pageSize == 0 ? data.size() / pageSize : data.size() / pageSize + 1;
        List<?> result = new ArrayList<>();
        try {
            currentPage = Integer.valueOf(page);
        } catch (NumberFormatException e) {
            currentPage = 1;
        }
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (currentPage <= pageCount) {
            int start = pageSize * (currentPage - 1);
            int end = currentPage == pageCount ? data.size() : start + pageSize;
            result = data.subList(start, end);
        }
        return new SuccessResponse(result, new PageVO(data.size(), pageSize, pageCount, currentPage));
    }
}
